package com.cdcas;

import java.util.Random;
import java.util.Scanner;

public class MathApp1 {
    private static final String[] ENCOURAGEMENT = {
            "🌟 干得漂亮！", "🚀 势如破竹！", "💡 天才思路！", "🎯 正中靶心！", "🔥 火力全开！"
    };
    private static final String[] TIPS = {
            "提示：记得角度转弧度哦",
            "提示：负数没有实平方根",
            "提示：四舍五入到整数",
            "提示：e≈2.71828",
            "提示：三角函数注意单位"
    };

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random rand = new Random();
        int score = 0;
        int questionCount = 0;
        int combo = 0;

        System.out.println("✨ 欢迎来到挑战赛！✨");
        System.out.println("▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂");
        System.out.println("规则说明：");
        System.out.println("- 回答数学问题获得分数");
        System.out.println("- 连续答对触发连击奖励");
        System.out.println("- 输入 'exit' 随时退出\n");

        while (true) {
            Object[] questionData = generateQuestion(rand);
            String question = (String) questionData[0];
            double answer = (double) questionData[1];
            boolean needInteger = (boolean) questionData[2];

            System.out.println("\n第 " + (++questionCount) + " 题 ➤ " + question);
            System.out.print("你的答案 ➤ ");

            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("exit")) {
                break;
            }

            try {
                double userAnswer = Double.parseDouble(input);
                boolean isCorrect = needInteger ?
                        (Math.abs(userAnswer - answer) < 0.001) :
                        (Math.abs(userAnswer - answer) < 0.01);

                if (isCorrect) {
                    score += 1 + combo/3; // 连击奖励
                    combo++;
                    System.out.println("\n\033[32m" + ENCOURAGEMENT[rand.nextInt(ENCOURAGEMENT.length)] +
                            " (+" + (1 + combo/3) + "分) 连击x" + combo + "\033[0m");
                } else {
                    combo = 0;
                    System.out.println("\n\033[31m❌ 正确答案是: " +
                            (needInteger ? (int)answer : String.format("%.2f", answer)) + "\033[0m");
                }
            } catch (NumberFormatException e) {
                combo = 0;
                System.out.println("\n\033[33m⚠ 请输入数字或'exit'退出 (" +
                        TIPS[rand.nextInt(TIPS.length)] + ")\033[0m");
            }
        }

        System.out.println("\n▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂");
        System.out.println("最终得分: " + score + " 分");
        System.out.println(getFinalComment(score));
        scanner.close();
    }

    private static Object[] generateQuestion(Random rand) {
        int type = rand.nextInt(6);
        boolean needInteger = false;
        String question = "";
        double answer = 0;

        switch (type) {
            case 0: // 平方根挑战
                int num = rand.nextInt(100) + 1;
                question = "√(" + num + ") 的值是多少？(保留两位小数)";
                answer = Math.sqrt(num);
                break;

            case 1: // 三角函数挑战
                int degrees = new int[]{30, 45, 60, 90, 180}[rand.nextInt(5)];
                double radians = Math.toRadians(degrees);
                answer = Math.sin(radians);
                question = "sin(" + degrees + "°) 等于多少？(保留两位小数)";
                break;

            case 2: // 极值挑战
                int a = rand.nextInt(100) - 50;
                int b = rand.nextInt(100) - 50;
                answer = Math.max(a, b);
                question = "max(" + a + ", " + b + ") = ?";
                needInteger = true;
                break;

            case 3: // 指数运算
                double exponent = rand.nextDouble() * 3;
                answer = Math.exp(exponent);
                question = "e^" + String.format("%.1f", exponent) + " 的值？(保留两位小数)";
                break;

            case 4: // 随机数范围
                double randomValue = Math.random();
                answer = Math.floor(randomValue * 100);
                question = "Math.random()*100 的整数部分？";
                needInteger = true;
                break;

            case 5: // 勾股定理
                int x = rand.nextInt(10) + 1;
                int y = rand.nextInt(10) + 1;
                answer = Math.hypot(x, y);
                question = "直角边为 " + x + " 和 " + y + " 的斜边长度？(保留两位小数)";
                break;
        }

        return new Object[]{question, answer, needInteger};
    }

    private static String getFinalComment(int score) {
        return score > 20 ? "🏆 数学大师！你征服了数字王国！" :
                score > 15 ? "🎓 天才学者！继续保持！" :
                        score > 10 ? "💪 潜力无限！继续努力！" :
                                score > 5 ? "✨ 不错的开始！再接再厉！" :
                                        "🌱 数学花园刚发芽，继续探索吧！";
    }
}
